package com.combetter.changxing.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.combetter.changxing.bindingconfig.Utils;
import com.combetter.changxing.collections.BindingCollectionAdapter;
import com.combetter.changxing.collections.ItemViewArg;

import java.util.List;

/**
 * Created by Mark.fan on 2018/5/15.
 */

public class BindingRecyclerViewAdapter<T> extends RecyclerView.Adapter implements BindingCollectionAdapter<T> {

    protected List<T> mItems;

    private LayoutInflater mInflater;

    private ItemViewArg mItemViewArg;

    public BindingRecyclerViewAdapter(ItemViewArg itemViewArg) {
        mItemViewArg = itemViewArg;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(parent.getContext());
        }
        ViewDataBinding binding = onCreateBinding(mInflater, mItemViewArg.layoutRes(), parent);
        final RecyclerView.ViewHolder holder = onCreateViewHolder(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        T item = mItems.get(position);
        ViewDataBinding binding = DataBindingUtil.getBinding(holder.itemView);
        onBindBinding(binding, mItemViewArg.bindingVariable(), mItemViewArg.layoutRes(), position, item);
    }

    private void onBindBinding(ViewDataBinding binding, int variable, int layoutRes, int position, T item) {
        boolean result = binding.setVariable(variable, item);
        if (!result) {
            Utils.throwMissingVariable(binding, variable, layoutRes);
        }
        binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return (mItems == null) ? 0 : mItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        mItemViewArg.select(position, mItems.get(position));
        return mItemViewArg.layoutRes();
    }

    @Override
    public void setItems(List<T> items) {
//        if (mItems == items) {
//            return;
//        }
        mItems = items;
        notifyDataSetChanged();
    }

    @Override
    public T getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public ViewDataBinding onCreateBinding(LayoutInflater inflater, int layoutId, ViewGroup viewGroup) {
        return DataBindingUtil.inflate(inflater, layoutId, viewGroup, false);
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewDataBinding binding) {
        return new BindingViewHolder(binding);
    }

    private static class BindingViewHolder extends RecyclerView.ViewHolder {
        public BindingViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
        }
    }
}
